﻿Imports PadovadoAPI
Imports System.Data
Imports System.IO

Partial Class WebUserControls_SchedaLocale
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            If String.IsNullOrEmpty(Request.QueryString("PK_Locale")) Then
                Response.Redirect("~/Default.aspx")
            Else
                CaricaPagina()
                CaricaRepeater()
            End If

        End If

    End Sub

    Public Sub CaricaRepeater()
        Dim sql As String = ""
        ' Try

        Dim ds As New DataSet
        Dim FK_Organizzatore As New Integer

        If Not String.IsNullOrEmpty(Request.QueryString("PK_Locale")) Then
            FK_Organizzatore = CInt(Request.QueryString("PK_Locale").ToString)
        End If

        Dim dbConnector As New DBObject
        Dim CurrentDate As DateTime = DateTime.Now.Date()


        'Dim rptEventis As New List(Of Repeater)
        'rptEventis.Add(RptEventi1)
        'rptEventis.Add(RptEventi2)

        '  For i As Integer = 0 To 1
        sql = "SELECT  convert(varchar,E.DataInizio,103) + ' ' +  E.NomeEvento as NomeEvento," & _
            " E.Priorita, E.Descrizione as DescrizioneEvento, " & _
          " CASE isnull(E.imgEvento, '') WHEN '' then MAX(T.TargetImgURLSmall) else E.imgEvento end as ImgEvento, " & _
          " convert(varchar,E.DataInizio,103) as DataInizio, " & _
          " convert(varchar,E.DataFine,103) as DataFine, E.Link, E.Brochure, " & _
          " CASE isnull(E.intCategoria, 100) " & _
                      "     WHEN 100 then 'http://www.padovado.it/locali-divertimenti/Generico/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 1 then 'http://www.padovado.it/locali-divertimenti/Concerto/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 2 then 'http://www.padovado.it/locali-divertimenti/Festa/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 3 then 'http://www.padovado.it/locali-divertimenti/Discoteca/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 4 then 'http://www.padovado.it/locali-divertimenti/Fiera/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 5 then 'http://www.padovado.it/locali-divertimenti/Sagra/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 6 then 'http://www.padovado.it/locali-divertimenti/Gita/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 7 then 'http://www.padovado.it/locali-divertimenti/Gratis/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 8 then 'http://www.padovado.it/locali-divertimenti/Mostra/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) end as LinkInterno, " & _
          " CASE isnull(E.link, '') WHEN '' then 0 else 1 end as LinkPresente, " & _
          " CASE isnull(E.Brochure, '') WHEN '' then 0 else 1 end as BrochurePresente, " & _
               " isnull(I.Strada,'') + ' ' + isnull(I.Civico,'') + ' ' + isnull(I.Comune,'') + ' ' + isnull(I.Presso,'') as Indirizzo, " & _
                 " MAX(T.TargetImgURLSmall) as TargetImgURLSmall, " & _
                 " A.Denominazione as Organizzatore, " & _
                 " ROW_NUMBER() OVER(ORDER BY E.DataInizio) AS 'RowNumber'" & _
                 " FROM Evento E " & _
                 " LEFT OUTER JOIN Indirizzo I ON I.PK_Indirizzo = FK_Indirizzo " & _
                 " LEFT OUTER JOIN Evento_Organizzatore EO ON PK_Evento = EO.FK_Evento " & _
                 " LEFT OUTER JOIN Organizzatore O ON O.PK_Organizzatore = EO.FK_Organizzatore " & _
                 " LEFT OUTER JOIN Anagrafica A ON A.PK_Anagrafica = O.FK_Anagrafica " & _
                 " LEFT OUTER JOIN Evento_Target ET ON ET.FK_Evento = PK_Evento " & _
                 " LEFT OUTER JOIN Target T ON T.PK_Target = ET.FK_Target " & _
                 " WHERE E.Deleted = 0 AND O.PK_Organizzatore = " & FK_Organizzatore & " AND E.DataInizio >= CAST('" & CurrentDate.ToString("yyyyMMdd") & "' AS DATE) " & _
                 " GROUP BY E.NomeEvento, E.Descrizione, E.DataInizio, E.DataFine, E.Link, I.Strada, I.Civico, I.Comune, I.Presso, PK_Evento, E.imgEvento, E.Brochure, E.Priorita," & _
                 " E.intCategoria, A.Denominazione" & _
                 " ORDER BY  E.Priorita, E.DataInizio "
        '  Throw New Exception(sql)


        ds = dbConnector.FillDS(sql, Nothing, 1)

        If Not IsNothing(ds) AndAlso ds.Tables.Count > 0 Then
            ds.Tables(0).Columns.Add("ItemTemplateId")
            ds.Tables(0).Columns.Add("ImageApriId")
            For Each dr As DataRow In ds.Tables(0).Rows

                dr("ItemTemplateId") = "ItemTemplate_" & dr("RowNumber")
                dr("ImageApriId") = "ImageApri_" & dr("RowNumber")
            Next
            ' Throw New Exception(ds.Tables.Count)
            RptEventi1.DataSource = ds
            RptEventi1.DataBind()
        End If

        Try


            Dim di As DirectoryInfo = New DirectoryInfo(Server.MapPath("/"))
            Dim dirPath As String = di.FullName & "localimages\" & LblNomeLocaleSecco.Text.Replace(" ", "_")
            ' Throw New Exception(dirPath)

            '  System.IO.Directory.GetFiles(Path.Combine(dirPath))
            Dim ImagesDirName() As String = System.IO.Directory.GetDirectories(Path.Combine(dirPath))
            ' Throw New Exception(ImagesDirName.Length)
            Dim dt As New DataTable
            dt.Columns.Add("ImgAddress")
            dt.Columns.Add("AlbumTitle")
            Dim li As String = ""
            For Each str As String In ImagesDirName
                If System.IO.Directory.GetFiles(str).Length > 0 Then
                    Dim strFirsFile = System.IO.Directory.GetFiles(str)(0)
                    strFirsFile = strFirsFile.Replace(di.FullName, "../").Replace("\", "/")
                    dt.Rows.Add(strFirsFile, str.Split("\")(str.Split("\").Length - 1).Replace("_", " "))
                End If

                'li =
                'aList.Add(li)
            Next
            RptAlbum.DataSource = dt
            RptAlbum.DataBind()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub CaricaPagina()
        Dim org As New Organizzatore


        org = Organizzatore.LoadFromPk(Request.QueryString("PK_Locale"))
        Me.Page.Title = org.FK_Anagrafica.DefinizioneCompleta

        Dim Keywords As New HtmlMeta
        Keywords.Name = "keywords"
        Keywords.Content = org.FK_Anagrafica.DefinizioneCompleta.Replace(" ", ",")
        Page.Header.Controls.Add(Keywords)

        Dim Description As New HtmlMeta
        Description.Name = "Description"
        Description.Content = org.FK_Anagrafica.DefinizioneCompleta
        Page.Header.Controls.Add(Description)

        LblNomeLocale.Text = org.FK_Anagrafica.DefinizioneCompleta
        LblNomeLocaleSecco.Text = org.FK_Anagrafica.Denominazione

        Select Case org.GiornoChiusura
            Case 1
                LblGiornoChiusura.Text = "Chiuso il lunedì"
            Case 2
                LblGiornoChiusura.Text = "Chiuso il martedì"
            Case 3
                LblGiornoChiusura.Text = "Chiuso il mercoledì"
            Case 4
                LblGiornoChiusura.Text = "Chiuso il giovedì"
            Case 5
                LblGiornoChiusura.Text = "Chiuso il venerdì"
            Case 6
                LblGiornoChiusura.Text = "Chiuso il sabato"
            Case 7
                LblGiornoChiusura.Text = "Chiuso la domenica"
            Case 1000
                LblGiornoChiusura.Text = "Apero tutti i giorni"

        End Select

        LblTipoLocale.Text = org.enTipoLocale.ToString

        If Not String.IsNullOrEmpty(org.imgLocaleString) Then
            ImgLocale.ImageUrl = org.imgLocaleString
        Else
            ImgLocale.Visible = False
        End If

        If Not String.IsNullOrEmpty(org.SitoWeb) Then
            HLSito.Text = "Sito locale"
            HLSito.Target = "blank"
            HLSito.NavigateUrl = org.SitoWeb
        Else
            HLSito.Visible = False
        End If

        LblDove.Text = org.FK_Indirizzo.DefinizioneCompleta


    End Sub

End Class
